import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import {resolve} from 'path'
import { createProxyMiddleware } from 'http-proxy-middleware';
const proxy = createProxyMiddleware({
  target: 'http://example.com',
  pathRewrite: {
    '/api': '/new/api',
  },
});
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  server:{
    port:8848,
    proxy:{
      '/api':{
        target:'https://duck.vin',
        changeOrigin:true,
        ws: true,  //是否代理 websockets
        secure: false, // 如果是https接口，需要配置这个参数
        rewrite:(path)=>path.replace(/^\/api/,''),
        
      },
      '/proxy': {
        target: '',
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/proxy/, ''),
        configure: (proxy, options) => {
          // Here you can set up custom configuration for the proxy
          proxy.on('proxyReq', (proxyReq, req, res) => {
            const targetUrl = req.url.replace(/^\/proxy/, '');
            proxyReq.path = targetUrl;
            proxyReq.setHeader('Host', new URL(targetUrl).host);
          });
        },
      },
    },
    
    cors:{
      allowedHeaders:'*',
    }
  },
  css:{
    preprocessorOptions:{
      scss:{
        additionalData:
        `
        @import "@/bem.scss";
        @import "@/themes/index.scss";
      `    
        }
      }
  },
  resolve:{
    //设置文件 ./src路径为@
    alias:[
      {
        find:'@',
        replacement:resolve(__dirname,'src')
      }
    ]
  }
})
